﻿namespace SEED.SEEDControllers {
    using System;

    public class Utility {
        
        public static short bytesToShort(byte highByte, byte lowByte, bool swapBytes) {
            if (swapBytes) {
                return (short)((highByte & 0xff) + (lowByte & 0xff) << 8);
            }
            else {
                return (short)(((lowByte & 0xff) << 8) + (lowByte & 0xff));
            }
        }

        public static int bytesToInt(byte a) {
            return (int)a;  
        }

        public static int bytesToInt(byte highByte, byte lowByte, bool swapBytes) {
            if (swapBytes) {
                return (highByte & 0xff) + ((int)lowByte << 8);
            }
            else {
                return ((int)highByte << 8) + (lowByte & 0xff);
            }
        }

        public static int bytesToInt(byte highestByte, byte secondHighestByte, byte lowestByte, bool swapBytes) {
            if (swapBytes) {
                return (highestByte & 0xff) + ((secondHighestByte & 0xff) << 8) + ((int)lowestByte << 16);
            }
            else {
                return ((int)highestByte << 16) + ((secondHighestByte & 0xff) << 8) + (lowestByte & 0xff);
            }
        }

        public static int bytesToInt(byte highestByte, byte secondHighestByte, byte secondLowestByte, byte lowestByte, bool swapBytes) {
            if (swapBytes) {
                return ((highestByte & 0xff)) + ((secondHighestByte & 0xff) << 8) + 
                       ((secondLowestByte & 0xff) << 16) + ((lowestByte & 0xff) << 24);
            }
            else {
                return ((highestByte & 0xff) << 24) + ((secondHighestByte & 0xff) << 16) + 
                       ((secondLowestByte & 0xff) << 8) + ((lowestByte & 0xff));
            }
        }

        public static int uBytesToInt(byte a) {
            return a & 0xff;
        }

        public static int uBytesToInt(byte highByte, byte lowByte, bool swapBytes) {
            
            if (swapBytes) {
                return (highByte & 0xff) + ((lowByte & 0xff) << 8);
            }
            else {
                return ((highByte & 0xff) << 8) + (lowByte & 0xff);
            }
        }

        public static long uBytesToLong(byte highestByte, byte secondHighestByte, byte secondLowestByte, byte lowestByte, bool swapBytes) {
            if (swapBytes) {
                return ((highestByte & 0xffL)) + ((secondHighestByte & 0xffL) << 8) +
                       ((secondLowestByte & 0xffL) << 16) + ((lowestByte & 0xffL) << 24);
            }
            else {
                return ((highestByte & 0xffL) << 24) + ((secondHighestByte & 0xffL) << 16) +
                       ((secondLowestByte & 0xffL) << 8) + ((lowestByte & 0xffL));
            }
        }

        public static byte[] longToIntBytes(long a) {
            byte[] returnByteArray = new byte[4]; //int is 4 bytes
            returnByteArray[0] = (byte)((a & 0xff000000) >> 24);
            returnByteArray[1] = (byte)((a & 0x00ff0000) >> 16);
            returnByteArray[2] = (byte)((a & 0x0000ff00) >> 8);
            returnByteArray[3] = (byte)((a & 0x000000ff));
            return returnByteArray;
        }

        public static byte[] intToShortBytes(int a) {
            byte[] returnByteArray = new byte[2];  //short is 2 bytes
            returnByteArray[0] = (byte)((a & 0x0000ff00) >> 8);
            returnByteArray[1] = (byte)((a & 0x000000ff));
            return returnByteArray;
        }

        public static byte[] pad(byte[] source, int requiredBytes, byte paddingByte) {
            if (source.Length >= requiredBytes) {
                return source;
            }
            else {
                byte[] returnByteArray = new byte[requiredBytes];
                Array.Copy(source, 0, returnByteArray, 0, source.Length);
                for (int i = source.Length; i < requiredBytes; i++) {
                    returnByteArray[i] = (byte)paddingByte;
                }
                return returnByteArray;
            }
        }

        public static byte[] format(byte[] source, int start, int end) {
            byte[] returnByteArray = new byte[start - end + 1];
            int j = 0;
            for (int i = start; i < end; i++, j++) {
                returnByteArray[j] = source[i];
            }
            return returnByteArray;
        }
    

    }
}
